﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LeadTeam.Exam.Entities;

namespace LeadTeam.Exam.AdminTool
{
    public partial class OrganizationsView : UserControl, IOrganizationsView
    {
        private OrganizationsControllor _controllor;

        public OrganizationsControllor Controllor
        {
            get { return _controllor; }
            set 
            {
                this._controllor = value;
                LoadModel();
            }
        }

        public OrganizationsView()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load mode data to the view
        /// </summary>
        public void LoadModel()
        {
            tvOrg.Nodes.Clear();

            IList<Organizations> list = _controllor.model;
            IList<Organizations> listRoot = list.Where(p => (p.O_PARENT_ID == 0)).ToList<Organizations>();
            foreach (Organizations org in listRoot)
            {
                TreeNode newNode = new TreeNode(org.O_NAME);
                newNode.Tag = org.O_ID;
                newNode.ImageIndex = 0;
                tvOrg.Nodes.Add(newNode);
                LookChildeNode(newNode, org.O_ID, list);
            }

            tvOrg.ExpandAll();
        }

        /// <summary>
        /// Private method to add nodes to the tree view
        /// </summary>
        /// <param name="parentNode"></param>
        /// <param name="id"></param>
        /// <param name="list"></param>
        private void LookChildeNode(TreeNode parentNode, int id,IList<Organizations> list)
        {
            foreach (Organizations org in list)
            {
                if (org.O_PARENT_ID == id)
                {
                    TreeNode newNode = new TreeNode(org.O_NAME);
                    newNode.Tag = org.O_ID;
                    newNode.ImageIndex = 0;
                    parentNode.Nodes.Add(newNode);

                    LookChildeNode(newNode,org.O_ID, list);
                }
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Controllor.model[0].O_NAME = "new Test";
        }

        /// <summary>
        /// IOrganizationsView member get the Organization id
        /// </summary>
        /// <returns></returns>
        public int GetSelectOrganizationId()
        {
            if (this.tvOrg.SelectedNode!=null)
            {
                return Convert.ToInt32(this.tvOrg.SelectedNode.Tag);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// IOrganizationsView member get the Organization Name
        /// </summary>
        /// <returns></returns>
        public string GetSelectOrganizationName()
        {
            if (tvOrg.SelectedNode != null)
            {
                return this.tvOrg.SelectedNode.Text;
            }
            else
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// IOrganizationsView member update the mode for the view
        /// </summary>
        /// <returns></returns>
        public void UpdateModel()
        {
            LoadModel();
        }


        public int GetSelectOrganizationParentParentId()
        {
            if (tvOrg.SelectedNode == null)
            {
                return 0;
            }
            return Convert.ToInt32(tvOrg.SelectedNode.Parent.Parent.Tag);
        }


        public bool HaveParentParent()
        {
            if (tvOrg.SelectedNode == null)
            {
                return false;
            }
            if (tvOrg.SelectedNode.Parent==null ||tvOrg.SelectedNode.Parent.Parent == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public int GetSelectOrganizationParentId()
        {
            if (tvOrg.SelectedNode == null)
            {
                return 0;
            }
            return Convert.ToInt32(tvOrg.SelectedNode.Parent.Tag);
        }

        public bool HaveParent()
        {
            if (tvOrg.SelectedNode == null)
            {
                return false;
            }
            if (tvOrg.SelectedNode.Parent == null || tvOrg.SelectedNode.Parent.Parent == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
